using ProtoModels;
using UnityEngine;

public class TournamentController : MonoBehaviour
{
	private void OnEnable()
	{
		if (IsSeasonIntervalFinished())
		{
			Debug.Log("advancing");
			AdvanceSeasonInterval();
		}
	}

	private bool IsSeasonIntervalFinished()
	{
		TournamentCareerStats careerStatsManager = GGPlayerSettings.instance.careerStatsManager;
		SeasonDefinitions.SeasonInterval intervalForIds = SeasonDefinitions.instance.GetIntervalForIds((SeasonDefinitions.SeasonId)careerStatsManager.TournamentCareer.activeSeasonInterval.seasonId, (SeasonDefinitions.SeasonIntervalId)careerStatsManager.TournamentCareer.activeSeasonInterval.seasonIntervalId);
		if (intervalForIds.eventsForInterval.Count > careerStatsManager.TournamentCareer.activeSeasonInterval.currentEvents.Count)
		{
			return false;
		}
		foreach (TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent currentEvent in careerStatsManager.TournamentCareer.activeSeasonInterval.currentEvents)
		{
			if (!currentEvent.isFinished)
			{
				return false;
			}
		}
		return true;
	}

	private void PrintCareerInfo()
	{
		TournamentCareerStats careerStatsManager = GGPlayerSettings.instance.careerStatsManager;
		Debug.Log(careerStatsManager.TournamentCareer.activeSeasonInterval.seasonId);
		Debug.Log(careerStatsManager.TournamentCareer.activeSeasonInterval.seasonIntervalId);
		foreach (TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent currentEvent in careerStatsManager.TournamentCareer.activeSeasonInterval.currentEvents)
		{
			Debug.Log(currentEvent.stageIndex);
			Debug.Log(currentEvent.isStarted);
			Debug.Log(currentEvent.isFinished);
			Debug.Log(currentEvent.eventInfo.namedEventId);
			Debug.Log(currentEvent.eventInfo.score);
		}
		foreach (TournamentCareerDAO.GenericEventStats genericTournamentStat in careerStatsManager.TournamentCareer.genericTournamentStats)
		{
			Debug.Log(genericTournamentStat.eventId);
			Debug.Log(genericTournamentStat.timesPlayed);
			Debug.Log(genericTournamentStat.numberOfWins);
			Debug.Log(genericTournamentStat.numberOfLosses);
		}
		foreach (TournamentCareerDAO.NamedEventPerformance namedTournamentPerformance in careerStatsManager.TournamentCareer.namedTournamentPerformances)
		{
			Debug.Log(namedTournamentPerformance.namedEventId);
			Debug.Log(namedTournamentPerformance.score);
		}
	}

	private void AdvanceSeasonInterval()
	{
		TournamentCareerStats careerStatsManager = GGPlayerSettings.instance.careerStatsManager;
		SeasonDefinitions.SeasonId seasonId = (SeasonDefinitions.SeasonId)careerStatsManager.TournamentCareer.activeSeasonInterval.seasonId;
		SeasonDefinitions.SeasonIntervalId seasonIntervalId = (SeasonDefinitions.SeasonIntervalId)careerStatsManager.TournamentCareer.activeSeasonInterval.seasonIntervalId;
		SeasonDefinitions.NextIntervalInfo infoForNextInterval = SeasonDefinitions.instance.GetInfoForNextInterval(seasonId, seasonIntervalId);
		careerStatsManager.BeginSeasonInterval(infoForNextInterval.season, infoForNextInterval.seasonInterval);
	}

	public void ReportEventStageFinished(SeasonDefinitions.NamedEventId eventId, TournamentCareerStats.EventScore score, bool isWin)
	{
		TournamentCareerStats careerStatsManager = GGPlayerSettings.instance.careerStatsManager;
		SeasonDefinitions.SeasonId seasonId = (SeasonDefinitions.SeasonId)careerStatsManager.TournamentCareer.activeSeasonInterval.seasonId;
		SeasonDefinitions.SeasonIntervalId seasonIntervalId = (SeasonDefinitions.SeasonIntervalId)careerStatsManager.TournamentCareer.activeSeasonInterval.seasonIntervalId;
		SeasonDefinitions.SeasonInterval intervalForIds = SeasonDefinitions.instance.GetIntervalForIds(seasonId, seasonIntervalId);
		SeasonDefinitions.SeasonEvent seasonEvent = null;
		foreach (SeasonDefinitions.SeasonEvent item in intervalForIds.eventsForInterval)
		{
			if (item.namedEventId == eventId)
			{
				seasonEvent = item;
				break;
			}
		}
		if (seasonEvent != null)
		{
			Events.EventDefinition eventById = EventsDB.instance.GetEventById(seasonEvent.eventDefinitionId);
			if (eventById != null)
			{
				TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent activeEvent = careerStatsManager.GetActiveEvent(seasonEvent.namedEventId);
				bool flag = !isWin || eventById.tournamentStages.Count <= 1 || (activeEvent != null && activeEvent.stageIndex >= eventById.tournamentStages.Count - 1);
				careerStatsManager.AdvanceActiveEvent(seasonEvent, score, isWin);
			}
		}
	}

	private bool IsAnyEventActive()
	{
		TournamentCareerStats careerStatsManager = GGPlayerSettings.instance.careerStatsManager;
		foreach (TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent currentEvent in careerStatsManager.TournamentCareer.activeSeasonInterval.currentEvents)
		{
			if (currentEvent.isStarted && !currentEvent.isFinished)
			{
				return true;
			}
		}
		return false;
	}
}
